package org.mogu;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.mogu.service.GetSyncUrl;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.CityData;
import org.mogu.tools.UrlSync;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class QQ_Search extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener{
	Spinner spin_sheng;
	Spinner spin_city;
	Spinner spin_age;
	CityData cityData=new CityData();
	RadioButton jingque;
	RadioButton tiaojian;
	EditText search_username;
	EditText search_nickname;
	RadioButton nosex;
	RadioButton girl;
	RadioButton boy;
	LinearLayout jingque_search;
	LinearLayout tiaojian_search;
	private UrlSync urlSync=new UrlSync();
	public Handler mMainHandler;
	private String maincode=null;
	private String code=null;
	Intent intent;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.qq_searchwindow);
		spin_sheng=(Spinner)findViewById(R.id.spinner_sheng);
		spin_sheng.setOnItemSelectedListener(this);
		spin_city=(Spinner)findViewById(R.id.spinner_city);
		spin_age=(Spinner)findViewById(R.id.spinner_age);
		jingque=(RadioButton)findViewById(R.id.jqradio);
		jingque.setOnClickListener(this);
		tiaojian=(RadioButton)findViewById(R.id.tiaojianradio);
		tiaojian.setOnClickListener(this);
		jingque_search=(LinearLayout)findViewById(R.id.jingque_search);
		tiaojian_search=(LinearLayout)findViewById(R.id.tiaojian_search);
		search_username=(EditText)findViewById(R.id.search_username);
		search_nickname=(EditText)findViewById(R.id.search_nickname);
		nosex=(RadioButton)findViewById(R.id.nosex);
		girl=(RadioButton)findViewById(R.id.girl);
		boy=(RadioButton)findViewById(R.id.boy);
		
		ArrayAdapter<String> sheng=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityData.sheng_search);
		sheng.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_sheng.setAdapter(sheng);
		
		ArrayAdapter<String> age=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityData.age);
		age.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_age.setAdapter(age);
		intent=this.getIntent();  
		Bundle bunde = intent.getExtras();
		maincode = bunde.getString("maincode");
		code = bunde.getString("code");
		
		mMainHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				// // 接收子线程的消息
				if(msg.arg1==1){
					UrlSync urlSync=(UrlSync)msg.obj;
					gotoSearchList(urlSync.getResult());
					
				}
				
			}

		};
	}
	public void gotoSearchList(String result){
		Intent intent = new Intent();
		intent.setClass(this, QQ_Search_List.class);
		Bundle bundle = new Bundle();
		bundle.putString("result",result);
		bundle.putString("maincode",maincode);
		bundle.putString("code",code);
		intent.putExtras(bundle);
//		intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
		startActivityForResult(intent, 0);
	}
	
	
	/*
     * 控件单击事件监听器接口函数
     * */
    @Override
    public void onClick(View view){
    	switch(view.getId()){
    		case R.id.jqradio:
    			jingque_search.setVisibility(View.VISIBLE);
    			tiaojian_search.setVisibility(View.GONE);
    			break;
    		case R.id.tiaojianradio:
    			jingque_search.setVisibility(View.GONE);
    			tiaojian_search.setVisibility(View.VISIBLE);
    			break;
    	}
	}
    
    public void searchFriend(View view){
    	List<NameValuePair> param=new ArrayList<NameValuePair>();
    	if(jingque.isChecked()){
    		if(search_username.getText().toString()==null||"".equals(search_username.getText().toString())){
    			Toast.makeText(this, "用户名不能为空。", 4000).show();
    			return;
    		}
    		param.add(new BasicNameValuePair("searchUname",search_username.getText().toString()));
    	}
    	if(tiaojian.isChecked()){
    		param.add(new BasicNameValuePair("nickname",search_nickname.getText().toString()));
    		if(spin_sheng.getSelectedItemPosition()!=0){
    			param.add(new BasicNameValuePair("sheng",String.valueOf(spin_sheng.getSelectedItemPosition())));
    		}
    		if(spin_city.getSelectedItemPosition()!=0){
    			param.add(new BasicNameValuePair("city",String.valueOf((spin_sheng.getSelectedItemPosition())*10).substring(0, 2)+String.valueOf(spin_city.getSelectedItemPosition())));
    		}
    		if(!nosex.isChecked()){
    			if(girl.isChecked()){
    				param.add(new BasicNameValuePair("sex",String.valueOf(true)));
        		}else{
    				param.add(new BasicNameValuePair("sex",String.valueOf(false)));
    			}
    		}
    		if(spin_age.getSelectedItemPosition()!=0){
    			String minage="0";
    			String maxage="99";
    			switch(spin_age.getSelectedItemPosition()){
	    			case 1:
	    				maxage="17";
	    				break;
	    			case 2:
	    				minage="18";
	    				maxage="24";
	    				break;
	    			case 3:
	    				minage="25";
	    				maxage="30";
	    				break;
	    			case 4:
	    				minage="31";
	    				maxage="35";
	    				break;
	    			case 5:
	    				minage="36";
	    				maxage="40";
	    				break;
	    			case 6:
	    				minage="41";
	    				maxage="50";
	    				break;
	    			case 7:
	    				minage="51";
	    				maxage="59";
	    				break;
	    			case 8:
	    				minage="60";
	    				break;
    			}
    			param.add(new BasicNameValuePair("minage",minage));
    			param.add(new BasicNameValuePair("maxage",maxage));
    		}
    	}
    	urlSync.setPrarm(param);
    	urlSync.setResultType("32");
		urlSync.setModth(UrlSync.POST);
		urlSync.setNotice(false);
		urlSync.setUri(GetSyncUrl.getAppUrlByCode(maincode)+"/Search");
		urlSync.setMaincode(maincode);
		urlSync.setSyncType(UrlSync.INFOSEND);
		urlSync.setHandler(mMainHandler);
//		urlSync.setCode(imUser.getCode());
		urlSync.setToastContentSu("查找用户成功了。");
		urlSync.setToastContentFa("查找用户失败了。");
		synchronized (Convert.class) {
			Convert.syncList.add(0,urlSync);
		}
		Toast.makeText(this, "正在提交。", 4000).show();
    }
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		ArrayAdapter<String> city=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityData.city_search.get(cityData.sheng_search[arg2]));
		city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_city.setAdapter(city);
		// TODO Auto-generated method stub
//		changeAdapter(groupidlist.get(arg2));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	public void onBack(View view) {
    	if(intent==null){
    		return;
    	}
    	this.setResult(RESULT_OK, intent);
    	this.finish(); 
    	
    }
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
//				) { // 按下的如果是BACK，同时没有重复
//
//			onBack();
//			return true;
//		}
//		return super.onKeyDown(keyCode, event);
//	}
}
